import { message as Message, Modal } from "ant-design-vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { computed, Ref, createVNode } from "vue";
import { customsClearanceApi, cancelMergeDeclarationApi } from "@/api/waybillManagement/customerServiceWaybill";
export default function CustomsClearanceHook(state: { loading: Ref<boolean>; tableRef: any; searchHandle: () => void }) {
  const { searchHandle, loading, tableRef } = state;
  const selectRows = computed<Array<ClientWaybillManagement.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  //合并报关
  const customsClearanceHandle = () => {
    if (selectRows.value.length < 2) return Message.error("至少勾选两条运单!");
    loading.value = true;
    return customsClearanceApi(selectRows.value.map((t: any) => t.id))
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          Message.success("操作成功!");
          searchHandle();
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  //取消合并报关
  const cancelMergeDeclarationHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    Modal.confirm({
      title: "对任一运单取消合并报关，关联合并报关号的所有运单都将取消合并报关,请谨慎操作！！",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        return cancelMergeDeclarationApi(selectRows.value.map((t: any) => t.id)).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            Message.success("操作成功!");
            searchHandle();
          }
        });
      }
    });
  };

  return {
    customsClearanceHandle,
    cancelMergeDeclarationHandle
  };
}
